﻿/******************************************************   
创建者: ljj
创建时间: 2017-11-16
修改:
修改时间: 
功能: 配件中包装导入
 ******************************************************/

using System;
using System.Collections.Generic;
using System.ComponentModel.Composition;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using com.nm;
using com.nm.library.Excels;

namespace VerticaAPI.Export
{
    /// <summary>
    /// 导入导出单店季SKC数量
    /// </summary>
    [Export(typeof(ExcelImport))]
    class SKCSizeType : ExcelImport
    {
        private static int num = 0;

        public override ExcelImportType Type => ExcelImportType.SKCSIZETYPE;
        public override Dictionary<string, ImportVerify> DictFields => _dictFields;

        private Dictionary<string, ImportVerify> _dictFields = new List<ImportVerify> {
            new ImportVerify{ ColumnName="类别",FieldName="KindName",DataType=typeof(string),VerifyFunc =(e,extra)=> ExcelImportHelper.GetCellMsg(e.CellValue,e.ColName,100,false,true)},
            new ImportVerify{ ColumnName="货号",FieldName="MaterialID",DataType=typeof(string),VerifyFunc =(e,extra)=> ExcelImportHelper.GetCellMsg(e.CellValue,e.ColName,100,true,true)},
            new ImportVerify{ ColumnName="系数",FieldName="SizeLimit",DataType=typeof(long),VerifyFunc =(e,extra)=> ExcelImportHelper.GetCellMsg(e.CellValue,e.ColName,100,true,true)},
        }.ToDictionary(e => e.ColumnName, e => e);

        public override object SaveImportData(DataTable dt, Dictionary<string, object> extraInfo, int userId)
        {
            var uid = Guid.NewGuid().ToString("N");
            var tempTableName = $" ndw.ndw_{uid} ";
            var handler = new SKCSizeTypeHandler();
            var result = new ImportResult
            {
                IsSuccess = true,
                ExtraInfo = "导入成功",
            };
            try
            {
                handler.DropTempTable(tempTableName);
                handler.CreateTempTable(dt, tempTableName);
                var errorDT = handler.ImportSKCSizeType(tempTableName);
                result.Message = $"成功导入{(dt.Rows.Count - errorDT.Rows.Count)}条数据{(errorDT.Rows.Count == 0 ? "" : $@",有{errorDT.Rows.Count}条错误数据")}";
                if (errorDT.Rows.Count != 0)
                {
                    var path = ExcelImporMapper.GetTemplatePath(Type);
                    result.Url = ExcelUtil.ExportErrorExeclToDB(errorDT, path, _dictFields, "配件中包装错误数据", num);
                    num++;
                }
            }
            catch (Exception ex)
            {
                Log.Error("导入单店季SKC数量：" + ex);
                result.IsSuccess = false;
                result.ExtraInfo = ex;
                result.Message = ex.Message;
            }
            finally
            {
                handler.DropTempTable(tempTableName);
            }
            return result;
        }

        public override DataTable GetExportData(string Keys)
        {
            throw new NotImplementedException();
        }
    }
}
